const axios = require('axios')
const cheerio = require('cheerio')
const fs = require('fs')
const path = require('path')
let flag = 0
function main (url) {
  axios({
    method: 'GET',
    url: `https:${url}`
  }).then(res => {
    flag++
    // console.log(res.data)
    // 想让给定的 html 格式的字符串转化为一个 DOM 文档（虚拟的）
    const $ = cheerio.load(res.data)
    $('.read-content p').each((index, item) => {
      const content = $(item).html()
      fs.appendFileSync(path.resolve(__dirname, `doc/${flag}-${$('.j_chapterName').text().trim()}.txt`), `${content}\r`)
    })
    if(flag === 10) return
    main($('#j_chapterNext').attr('href'))
    // console.log($('#j_chapterNext').attr('href'))
  })
}
main('//read.qidian.com/chapter/t-HRFaCDKWrOVUeyz9PqUQ2/JLTu64Ss5itOBDFlr9quQA2/')